package org.jeecg.modules.business.third.strategy;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.business.third.strategy.emsdms.ThirdOperateStrategy;
import org.jeecg.modules.business.third.strategy.emsdms.ThirdOperateStrategyContext;
import org.jeecg.modules.business.third.strategy.sap.SapOperateStrategy;
import org.jeecg.modules.business.third.strategy.sap.SapOperateStrategyContext;
import org.jeecg.modules.business.third.util.CommonUtil;
import org.jeecg.modules.flow.sys.util.SpringContextUtil;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.regex.Pattern;

@Slf4j
@Component
public class StrategyMapInitApplicationRunner implements ApplicationRunner {

    public static String getKey(String str) {
        String key = "";
        if (isNumeric(str.substring(3, 4))) {
            key = str.substring(0, 4).toUpperCase();
        } else {
            key = str.substring(0, 3).toUpperCase();
        }
        return key;
    }

    //方法三：
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<String> tableNameList = CommonUtil.getTableNameList("org.jeecg.modules.business.third.strategy.emsdms");
        for (String str : tableNameList) {
            String key = getKey(str);
            ThirdOperateStrategyContext.strategyMap.put(key, (ThirdOperateStrategy) SpringContextUtil.getBean(str));
        }

        List<String> tableNameList1 = CommonUtil.getTableNameList("org.jeecg.modules.business.third.strategy.sap");
        for (String str : tableNameList1) {
            String key = getKey(str);
            SapOperateStrategyContext.strategyMap.put(key, (SapOperateStrategy) SpringContextUtil.getBean(str));
        }
    }

}